package com.cuitrip.business.tripservice.ui;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.cuitrip.business.tripservice.model.SelectMenu;
import com.cuitrip.component.wheel.OnWheelChangedListener;
import com.cuitrip.component.wheel.OnWheelScrollListener;
import com.cuitrip.component.wheel.WheelView;
import com.cuitrip.component.wheel.adapters.WheelViewAdapter;
import com.cuitrip.service.R;
import com.lab.component.common.BaseDialogFragment;
import com.lab.utils.ArrayUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * 人数选择器
 * Created by ak on 2014/12/24.
 */
public class SelectTwoMenuFragment extends BaseDialogFragment implements View.OnClickListener{

    @Bind(R.id.wheel_view_1)
    WheelView firstWheelView;

    @Bind(R.id.wheel_view_2)
    WheelView secondWheelView;

    @Bind(R.id.btnCancel)
    Button btnCancel;

    @Bind(R.id.btnConfirm)
    Button btnConfirm;

    @Bind(R.id.title)
    TextView titleView;

    private boolean firstWheelIsScroll = false;

    private String title;

    private SelectWheelAdapter firstWheelAdapter;
    private List<SelectMenu> firstSelectMenuList = new ArrayList<>();

    private SelectWheelAdapter secondWheelAdapter;
    private List<SelectMenu> secondSelectMenuList = new ArrayList<>();

    private OnDataSource mOnDataSource;

    public void setOnDataSource(OnDataSource onDataSource){
        this.mOnDataSource = onDataSource;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.contextMenu);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.two_menu_dialog_layout, container, false);
        ButterKnife.bind(this, view);
        btnCancel.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        if (!TextUtils.isEmpty(title)){
            titleView.setText(title);
        }
        firstWheelView.setVisibleItems(5);
        secondWheelView.setVisibleItems(5);
        if (mOnDataSource == null){
            hideDialogFragment(this);
        }else{
            firstSelectMenuList.addAll(mOnDataSource.onFirstSource());
            secondSelectMenuList.addAll(mOnDataSource.onSecondSource(0,firstSelectMenuList.get(0)));
        }
        firstWheelAdapter = new SelectWheelAdapter(firstSelectMenuList);
        secondWheelAdapter = new SelectWheelAdapter(secondSelectMenuList);
        firstWheelView.setViewAdapter(firstWheelAdapter);
        secondWheelView.setViewAdapter(secondWheelAdapter);

        firstWheelView.addChangingListener(changedListener);
        firstWheelView.addScrollingListener(onWheelScrollListener);

        return view;
    }

    OnWheelChangedListener changedListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!firstWheelIsScroll) {
                updateSecondMenu();
            }
        }
    };

    OnWheelScrollListener onWheelScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
            firstWheelIsScroll = true;
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            updateSecondMenu();
        }
    };


    private void updateSecondMenu(){
        int position = firstWheelView.getCurrentItem();
        List<SelectMenu> selectMenuList = mOnDataSource.onSecondSource(position, firstWheelAdapter.getCurrentItem(position));
        secondSelectMenuList.clear();
        secondSelectMenuList.addAll(selectMenuList);
        secondWheelView.invalidateWheel(true);
        secondWheelView.setCurrentItem(0);
    }

    @OnClick(R.id.context_menu_blank)
    void onBlankClick(){
        hideDialogFragment(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnConfirm:
                int firstIndex = firstWheelView.getCurrentItem();
                int secondIndex = secondWheelView.getCurrentItem();
                SelectMenu firstSelectMenu = firstWheelAdapter.getCurrentItem(firstIndex);
                SelectMenu secondSelectMenu = secondWheelAdapter.getCurrentItem(secondIndex);
                mDataSendBackListenr.onSelect(firstSelectMenu, secondSelectMenu);
                hideDialogFragment(this);
                break;
            case R.id.btnCancel:
                hideDialogFragment(this);
                break;
        }

    }

    private class SelectWheelAdapter implements WheelViewAdapter {


        private List<SelectMenu> selectMenus = new ArrayList<>();

        public SelectWheelAdapter(List<SelectMenu> selectMenuList) {
            this.selectMenus = selectMenuList;
        }

        public SelectMenu getCurrentItem(int position){
            if (ArrayUtil.isIndexSafe(position, selectMenus)){
                return selectMenus.get(position);
            }else{
                return null;
            }
        }

        @Override
        public int getItemsCount() {
            return selectMenus == null ? 0 : selectMenus.size();
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gender_show, parent, false);
            }
            ((TextView) convertView).setText(selectMenus.get(index).value);
            ((TextView) convertView).setTextSize(17.f);
            return convertView;
        }

        @Override
        public View getEmptyItem(View convertView, ViewGroup parent) {
            return null;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }
    }

    public interface OnTwoDataSelectBackListener {
        public void onSelect(SelectMenu firstSelectMenu, SelectMenu secondSelectMenu);
    }

    private OnTwoDataSelectBackListener mDataSendBackListenr;

    public void setResultListener(OnTwoDataSelectBackListener listener) {
        mDataSendBackListenr = listener;
    }

    public interface OnDataSource{

        public List<SelectMenu> onFirstSource();

        public List<SelectMenu> onSecondSource(int index, SelectMenu firstMenu);

    }


}
